package com.map.application;


import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import org.eclipse.wb.swing.FocusTraversalOnArray;

import com.map.application.headquarter.HeadQuarterMainWindow;

import java.awt.Component;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login {

	private JFrame loginFrame;
	private JTextField usernameTextField;
	private JPasswordField passwordField;


	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		loginFrame = new JFrame();
		loginFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(Login.class.getResource("/resources/earth_icon.png")));
		loginFrame.setTitle("Map Service Center System");
		loginFrame.setBounds(100, 100, 450, 300);
		loginFrame.setLocationRelativeTo(null);
		loginFrame.setResizable(false);
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel lblUsername = new JLabel("Username :");
		
		JLabel lblPassword = new JLabel("Password :");
		
		usernameTextField = new JTextField();
		usernameTextField.setColumns(10);
		
		passwordField = new JPasswordField();
		
		JButton btnLogin = new JButton("Login");		
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println(usernameTextField.getText() +"|"+ new String(passwordField.getPassword()));
				if(usernameTextField.getText().equals("group2") && new String(passwordField.getPassword()).equals("pass")){
//				if(true){
					getLoginFrame().setVisible(false);
					HeadQuarterMainWindow headQuarterMainWindow = new HeadQuarterMainWindow(); 
					headQuarterMainWindow.getFrame().setVisible(true);					
				}
				else{
					JOptionPane.showMessageDialog(null, "Wrong Password !!", "alert", JOptionPane.ERROR_MESSAGE);
					clearTextBox();
				}
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearTextBox();
			}
		});
		
		JLabel lblMapServiceCenter = new JLabel("Map Service Center System");
		lblMapServiceCenter.setFont(new Font("Tahoma", Font.PLAIN, 30));
		lblMapServiceCenter.setHorizontalAlignment(SwingConstants.CENTER);
		GroupLayout groupLayout = new GroupLayout(loginFrame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(99)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblUsername)
								.addComponent(lblPassword))
							.addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(passwordField)
								.addComponent(usernameTextField, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED, 33, GroupLayout.PREFERRED_SIZE)
							.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnCancel, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
							.addGap(28)))
					.addGap(109))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addComponent(lblMapServiceCenter, GroupLayout.PREFERRED_SIZE, 393, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(20, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblMapServiceCenter, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblUsername)
						.addComponent(usernameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(33)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPassword)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnLogin)
						.addComponent(btnCancel))
					.addContainerGap(73, Short.MAX_VALUE))
		);
		loginFrame.getContentPane().setLayout(groupLayout);
		loginFrame.getContentPane().setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{usernameTextField, passwordField, btnLogin, btnCancel, lblUsername, lblPassword}));
	}

	public JFrame getLoginFrame() {
		return loginFrame;
	}
	
	public void clearTextBox(){
		usernameTextField.setText("");
		passwordField.setText("");
	}
	
	
}
